package com.JackTang.fruit;


/**
 * @author Jack Tang
 * @description
 * @create 2023-05-09 15:10
 */
public class Test {
    public static void main(String[] args) {
        Invoice t = new Invoice();
        Invoice ticket;
        ticket = new HeadDecorator(new FootDecorator(t));
        ticket.printInvoice();
        System.out.println("----------------------------");
        ticket = new HeadDecorator(new FootDecorator(null));
        ticket.printInvoice();
    }
}

class Invoice{
    public void printInvoice() {
        System.out.println("This is the content of the invoice!");
    }
}

class Decorator extends  Invoice {
    protected Invoice ticket;
    public Decorator(Invoice t){
        this.ticket = t;
    }

    public void printInvoice() {
        if (ticket != null){
            ticket.printInvoice();
        }
    }
}

class HeadDecorator extends Decorator {
    public HeadDecorator(Invoice t) {
        super(t); // 调用父类也就是 Decorator 的构造函数
    }
    public void printInvoice() {
        System.out.println("This is the header of the invoice!");
        super.printInvoice(); // 调用 Decorator 的方法
    }

}

class FootDecorator extends Decorator {
    public FootDecorator(Invoice t) {
        super(t);
    }
    public void printInvoice() {
        super.printInvoice();
        System.out.println("This is the footnote of the invoice!");
    }
}